package com.ruoyi.purchase.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.purchase.domain.ChangeDetail;
import com.ruoyi.purchase.service.IChangeDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * detailController
 * 
 * @author kevinness
 * @date 2025-01-05
 */
@RestController
@RequestMapping("/changeDetail")
public class ChangeDetailController extends BaseController
{
    @Autowired
    private IChangeDetailService changeDetailService;

    /**
     * 查询detail列表
     */
    @GetMapping("/list")
    public TableDataInfo list(ChangeDetail changeDetail)
    {
        startPage();
        List<ChangeDetail> list = changeDetailService.selectChangeDetailList(changeDetail);
        return getDataTable(list);
    }

    /**
     * 导出detail列表
     */
    @Log(title = "detail", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ChangeDetail changeDetail)
    {
        List<ChangeDetail> list = changeDetailService.selectChangeDetailList(changeDetail);
        ExcelUtil<ChangeDetail> util = new ExcelUtil<ChangeDetail>(ChangeDetail.class);
        util.exportExcel(response, list, "detail数据");
    }

    /**
     * 获取detail详细信息
     */
    @GetMapping(value = "/{orderNo}")
    public AjaxResult getInfo(@PathVariable("orderNo") String orderNo)
    {
        return success(changeDetailService.selectChangeDetailByOrderNo(orderNo));
    }

    /**
     * 新增detail
     */
    @Log(title = "detail", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ChangeDetail changeDetail)
    {
        return toAjax(changeDetailService.insertChangeDetail(changeDetail));
    }

    /**
     * 修改detail
     */
    @Log(title = "detail", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ChangeDetail changeDetail)
    {
        return toAjax(changeDetailService.updateChangeDetail(changeDetail));
    }

    /**
     * 按照id删除批量删除
     */
    @DeleteMapping("/{ids}")
    public AjaxResult deleteByIds(@PathVariable Integer[] ids){
        return toAjax(changeDetailService.deleteChangeDetailByIds(ids));
    }

    /**
     * 通过变更申请后更新订单信息
     */
    @PostMapping("/pass")
    public AjaxResult passChangeDetail(@RequestBody Integer id){
        return toAjax(changeDetailService.passChangeDetail(id));
    }
}
